var Bpmn = require('bpmn-engine');
var EventEmitter = require('events').EventEmitter;
var engine;
var activityIds = [];
var lineIds = [];
var curNodeIds = [];
var callbackFun;
var dataCs;
var errCs;
var listener = new EventEmitter();
function setListener() {
    listener.on('taken', function(flow){
        lineIds.push(flow.id);
    });
    for (var i = 0; i < dataCs['nodeIds'].length; i++) {
        listener.once('wait-' + dataCs['nodeIds'][i], function(child, instance) {
            activityIds.push(child.activity.id);
            instance.signal(child.activity.id, {
                sirname: 's'
            });
        });
    }
    return listener;
}
setTimeout(function(){
    try {
        var activitys = engine.getPendingActivities().definitions[0].children;
        for (var i = 0; i < activitys.length; i++) {
            curNodeIds.push(activitys[i].id);
        }
    }catch(e) {

    }
    var dddd = { activityIds: activityIds, curNodeIds: curNodeIds, state: '', lineIds: lineIds };
    callbackFun(errCs, JSON.stringify(dddd));
}, 400);
function FdaxyTask (data, callback) {
    callbackFun = callback;
    dataCs = data;
    setListener();
    engine = new Bpmn.Engine({
        name: data.engineName,
        source: data.engineSource
    });
    engine.execute({
        variables: data.variables,
        listener: listener
    }, function (err, definition) {
        errCs = err;
        if (err) {
            var dddd = { activityIds: [], curNodeIds: [], state: '', lineIds: [] };
            callbackFun(errCs, JSON.stringify(dddd));
        }
    });
};